/** 
 * Date:2016年6月4日下午12:29:39 
 * Copyright (c) 2016, benchone@163.com All Rights Reserved. 
 * 
 */
package com.example.iqiyitest.view;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

public class MovieViewAnimation {

	private static MovieViewAnimation instance;
	/**
	 * 焦点的动画
	 */
	protected ValueAnimator iconFocusAnimator = ValueAnimator.ofFloat(1);
	/**
	 * 正在失去焦点的View
	 */
	private MovieView lostFocusView;
	/**
	 * 正在获取焦点的View
	 */
	private MovieView gainFocusView;
	
	public static final float ICON_SCALE_FLOAT = 1.12F;
	public static final int ICON_ANIM_TIMEOUT = 300;

	public static MovieViewAnimation getInstance() {
		if (null == instance) {
			instance = new MovieViewAnimation();
		}
		return instance;
	}

	public MovieViewAnimation() {
		// 焦点动画
		iconFocusAnimator.setDuration(ICON_ANIM_TIMEOUT);
		iconFocusAnimator.setInterpolator(new OvershootInterpolator());
		iconFocusAnimator.addUpdateListener(new IconFocusAnimatorUpdateListener());
		iconFocusAnimator.addListener(new IconFocusAnimatorListener());
	}
	
	/**
	 * 图标焦点动画的AnimatorUpdateListener
	 */
	private final class IconFocusAnimatorUpdateListener implements AnimatorUpdateListener {
		@Override
		public void onAnimationUpdate(ValueAnimator animation) {
			float value = (Float) animation.getAnimatedValue();
			if (gainFocusView != null) {
				float animVal = 1 + (ICON_SCALE_FLOAT - 1) * 1000F * value / 1000F;
				gainFocusView.setObjectAnim(getObjectAnim(gainFocusView, animVal));
			}
			if (lostFocusView != null) {
				float animVal = ICON_SCALE_FLOAT - (ICON_SCALE_FLOAT - 1)
						* value;
				lostFocusView.setObjectAnim(getObjectAnim(lostFocusView, animVal));
			}
		}
	}

	/**
	 * 图标焦点动画的AnimatorListener
	 */
	private class IconFocusAnimatorListener implements AnimatorListener {
		@Override
		public void onAnimationStart(Animator animation) {
		}

		@Override
		public void onAnimationRepeat(Animator animation) {
		}

		@Override
		public void onAnimationEnd(Animator animation) {
			gainFocusView = null;
			lostFocusView = null;
		}

		@Override
		public void onAnimationCancel(Animator animation) {
			cancelAnimation(gainFocusView);
			cancelAnimation(lostFocusView);
		}

		/**
		 * 取消图标的焦点动画
		 * @param icon Icon
		 */
		private void cancelAnimation(View v) {
			if (v == null)
				return;
			v.setScaleX(1);
			v.setScaleY(1);
		}
	}

	public void doAnimation(MovieView view, boolean focus) {
		if (focus) {
			if (view == lostFocusView)
				lostFocusView = null;
			gainFocusView = view;
		} else {
			// 正在执行获得焦点动画时又失去了焦点
			if (view == gainFocusView)
				iconFocusAnimator.end();
			lostFocusView = view;
		}
		if (!iconFocusAnimator.isStarted())
			iconFocusAnimator.start();
	}
	
	public void doAnimation2(MovieView view, boolean focus){
		ImageView iv_poster = view.getIv_poster();
		TextView tv_bak = view.getTv_bak();
		float ty = -iv_poster.getHeight() * (ICON_SCALE_FLOAT - 1) / 2;
		if (focus) {
			ObjectAnimator.ofFloat(iv_poster, "scaleX", 1f, ICON_SCALE_FLOAT).setDuration(ICON_ANIM_TIMEOUT).start();
			ObjectAnimator.ofFloat(iv_poster, "scaleY", 1f, ICON_SCALE_FLOAT).setDuration(ICON_ANIM_TIMEOUT).start();
			ObjectAnimator.ofFloat(iv_poster, "translationY", 0f, ty).setDuration(ICON_ANIM_TIMEOUT).start();
			ObjectAnimator.ofFloat(tv_bak, "alpha", 0f, 1f).setDuration(ICON_ANIM_TIMEOUT).start();
		} else {
			ObjectAnimator.ofFloat(iv_poster, "scaleX", ICON_SCALE_FLOAT, 1f).setDuration(ICON_ANIM_TIMEOUT).start();
			ObjectAnimator.ofFloat(iv_poster, "scaleY", ICON_SCALE_FLOAT, 1f).setDuration(ICON_ANIM_TIMEOUT).start();
			ObjectAnimator.ofFloat(iv_poster, "translationY", ty, 0f).setDuration(ICON_ANIM_TIMEOUT).start();
			ObjectAnimator.ofFloat(tv_bak, "alpha", 1f, 0f).setDuration(ICON_ANIM_TIMEOUT).start();
		}
	}

	public float[] getObjectAnim(MovieView view, float val) {
		float[] objectAnim = new float[2];
		objectAnim[0] = val;
		// 计算上移距离
		objectAnim[1] = -view.getIv_poster().getHeight() * (val - 1) / 2;
		// 背景阴影显隐
		return objectAnim;
	}
	
	/***
	 * 抖动动画
	 * @param v 要抖动的控件
	 */
	public void shakeAnimation(View view) {
		int delta = 10;  
	    PropertyValuesHolder pvhTranslateX = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,  
	            Keyframe.ofFloat(0f, 0),  
	            Keyframe.ofFloat(.10f, -delta),  
	            Keyframe.ofFloat(.26f, delta),  
	            Keyframe.ofFloat(.42f, -delta),  
	            Keyframe.ofFloat(.58f, delta),  
	            Keyframe.ofFloat(.74f, -delta),  
	            Keyframe.ofFloat(.90f, delta),  
	            Keyframe.ofFloat(1f, 0f)  
	    );  
	    ObjectAnimator.ofPropertyValuesHolder(view, pvhTranslateX).setDuration(500).start();  
	}
}
